package com.siyu.preplan.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.common.config.change.ChangeConfigProperties;
import com.siyu.common.config.minio.MinIOConfigProperties;
import com.siyu.common.constant.MinioOrOssConstants;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.common.utils.aliyun.service.FileOssStorageService;
import com.siyu.common.utils.minio.service.FileStorageService;
import com.siyu.common.utils.uuid.IdUtils;
import com.siyu.preplan.dto.*;
import com.siyu.preplan.planpcondition.domain.PlanPCondition;
import com.siyu.preplan.planpcondition.service.impl.PlanPConditionServiceImpl;
import com.siyu.preplan.planpdepot.domain.PlanPDepot;
import com.siyu.preplan.planpdepot.service.IPlanPDepotService;
import com.siyu.preplan.planpdocument.service.impl.PlanPDocumentServiceImpl;
import com.siyu.preplan.planpdtml.domain.PlanPDtml;
import com.siyu.preplan.planpdtml.service.IPlanPDtmlService;
import com.siyu.preplan.planpfbase.domain.PlanPFbase;
import com.siyu.preplan.planpfbase.service.IPlanPFbaseService;
import com.siyu.preplan.planpmaterial.service.impl.PlanPMaterialServiceImpl;
import com.siyu.preplan.planpplan.domain.PlanPPlan;
import com.siyu.preplan.planpplan.service.impl.PlanPPlanServiceImpl;
import com.siyu.preplan.service.PlanPnewService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PlanPnewServiceImpl implements PlanPnewService {

    private static Boolean minioOrOss = MinioOrOssConstants.getMinioOrOss();

    @Autowired
    private IPlanService planService;

    @Autowired
    private PlanPMaterialServiceImpl planPMaterialService;

    @Autowired
    private PlanPDocumentServiceImpl planPDocumentService;

    @Autowired
    private PlanPConditionServiceImpl planPConditionService;

    @Autowired
    private PlanPPlanServiceImpl planPPlanService;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;

    @Autowired
    private FileOssStorageService fileOssStorageService;

    @Autowired
    private IPlanPFbaseService planPFbaseService;

    @Autowired
    private IPlanPDtmlService planPDtmlService;

    @Autowired
    private IPlanPDepotService planPDepotService;



    @Override
    @Transactional
    public LinkedHashMap<String, String> insertPrePlan(PrePlanDto prePlanDto) {
        // 方案新增
        Plan plan = new Plan();
        // 生成唯一id
        String id = IdUtils.getId();
        BeanUtil.copyProperties(prePlanDto, plan);
        plan.setId(id);
        plan.setCreateTime(new Date());
        plan.setPreType("预案");
        plan.setRscd(prePlanDto.getRscd());
        planService.insertPlan(plan);

        List<PlanPrePlanDto> planPrePlans = prePlanDto.getPlanPrePlans();
        ArrayList<PlanPPlan> planPPlans = new ArrayList<>();
        ArrayList<PlanPCondition> planPConditions = new ArrayList<>();
        ArrayList<PlanPFbase> planPFbasessaves = new ArrayList<>();
        ArrayList<PlanPDepot> planPDepots = new ArrayList<>();
        ArrayList<PlanPDtml> planPDtmls = new ArrayList<>();

        LinkedHashMap<String, String> planPPlanIds = new LinkedHashMap<>();

        // 处理新建预案信息
        for (int i = 0; i < planPrePlans.size(); i++) {
            PlanPrePlanDto planPrePlanDto = planPrePlans.get(i);
            PlanPPlan planPPlan = new PlanPPlan();
            BeanUtil.copyProperties(planPrePlanDto, planPPlan);
            planPPlan.setId(IdUtils.getId());
            planPPlan.setPlanId(plan.getId());
            planPPlans.add(planPPlan);
            // 存储PlanPPlan对象id
            planPPlanIds.put((i + 1 + "级响应预案"), planPPlan.getId());
            List<PlanPCondition> triggerConditions = planPrePlanDto.getTriggerConditions();

            // 处理村庄基础信息保存
            PlanPFbaseDto planPFbaseDto = planPrePlanDto.getPlanPFbase();
            if (planPFbaseDto != null) {
                PlanPFbase planPFbase = PlanPFbase.builder()
                        .iPg(planPFbaseDto.getIdPg())
                        .iH(planPFbaseDto.getIdH())
                        .iPpl(planPFbaseDto.getIdPpl())
                        .iArea(planPFbaseDto.getIdArea())
                        .vName(planPFbaseDto.getVaName())
                        .id(IdUtils.getId())
                        .planId(planPPlan.getId())
                        .build();
                planPFbasessaves.add(planPFbase);
            }


            // 处理阈值条件
            for (int j = 0; j < triggerConditions.size(); j++) {
                PlanPCondition planPCondition = triggerConditions.get(j);
                planPCondition.setId(IdUtils.getId());
                planPCondition.setPlanId(planPPlan.getId());
                planPConditions.add(planPCondition);
            }


            //处理仓库集合信息
      /*      List<PlanPDepotDto> planPDepotDtos = planPrePlanDto.getPlanPDepotDtos();
            for (PlanPDepotDto planPDepotDto : planPDepotDtos) {
                PlanPDepot planPDepot = new PlanPDepot();
                BeanUtil.copyProperties(planPDepotDto,planPDepot);
                planPDepot.setId(IdUtils.getId());
                planPDepot.setMUnit(planPDepotDto.getMunit());
                planPDepot.setPlanId(planPPlan.getId());
                planPDepots.add(planPDepot);

                // 处理仓库物资信息
                List<PlanPDtmlDto> planPDtmlDtos = planPDepotDto.getPlanPDtmlDtos();
                for (PlanPDtmlDto planPDtmlDto : planPDtmlDtos) {
                    PlanPDtml planPDtml = new PlanPDtml();
                    BeanUtil.copyProperties(planPDtmlDto,planPDtml);
                    planPDtml.setId(IdUtils.getId());
                    planPDtml.setPlanId(planPDepot.getId());
                    planPDtml.setDepotName(planPDepot.getDepotName());
                    planPDtmls.add(planPDtml);
                }
            }*/

        }
        planPPlanService.saveBatch(planPPlans);
        planPConditionService.saveBatch(planPConditions);
        planPFbaseService.saveBatch(planPFbasessaves);

     /*   planPDepotService.saveBatch(planPDepots);
        planPDtmlService.saveBatch(planPDtmls);*/

        return planPPlanIds;
    }


    @Override
    public AjaxResult uploadMapFile(MultipartFile safe, MultipartFile danger, MultipartFile aftercare, MultipartFile route, MultipartFile matter, String id) throws IOException {

        try {
            // 查询响应预案
            PlanPPlan planPPlan = planPPlanService.getById(id);
            if (safe != null) {
                String filename = id + "_safe.geojson";
                String pathUrl = "";
                if (minioOrOss) {
                    pathUrl = fileOssStorageService.uploadGeoJsonFile("plangeo/safe", filename, safe.getInputStream());
                } else {
                    pathUrl = fileStorageService.uploadGeoJsonFile("plangeo/safe", filename, safe.getInputStream());
                }
                planPPlan.setSafe(pathUrl);
            }
            if (danger != null) {
                String filename = id + "_danger.geojson";
                String pathUrl = "";
                if (minioOrOss) {
                    pathUrl = fileOssStorageService.uploadGeoJsonFile("plangeo/danger", filename, danger.getInputStream());
                } else {
                    pathUrl = fileStorageService.uploadGeoJsonFile("plangeo/danger", filename, danger.getInputStream());
                }
                planPPlan.setDanger(pathUrl);
            }
            if (aftercare != null) {
                String filename = id + "_aftercare.geojson";
                String pathUrl = "";
                if (minioOrOss) {
                    pathUrl = fileOssStorageService.uploadGeoJsonFile("plangeo/aftercare", filename, aftercare.getInputStream());
                } else {
                    pathUrl = fileStorageService.uploadGeoJsonFile("plangeo/aftercare", filename, aftercare.getInputStream());
                }
                planPPlan.setAftercare(pathUrl);
            }
            if (route != null) {
                String filename = id + "_route.geojson";
                String pathUrl = "";
                if (minioOrOss) {
                    pathUrl = fileOssStorageService.uploadGeoJsonFile("plangeo/route", filename, route.getInputStream());
                } else {
                    pathUrl = fileStorageService.uploadGeoJsonFile("plangeo/route", filename, route.getInputStream());
                }
                planPPlan.setRoute(pathUrl);
            }
            if (matter != null) {
                String filename = id + "_route.geojson";
                String pathUrl = "";
                if (minioOrOss) {
                    pathUrl = fileOssStorageService.uploadGeoJsonFile("plangeo/matter", filename, matter.getInputStream());
                } else {
                    pathUrl = fileStorageService.uploadGeoJsonFile("plangeo/matter", filename, matter.getInputStream());
                }
                planPPlan.setMatter(pathUrl);
            }

            planPPlanService.updatePlanDPlan(planPPlan);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("geojson文件上传失败!");
        }
        return AjaxResult.success("geojson文件上传成功!");
    }

    @Override
    @Transactional
    public void createDepot(PlanPDepotDto planPDepotDto) {

        PlanPDepot planPDepot = new PlanPDepot();
        BeanUtil.copyProperties(planPDepotDto, planPDepot);
        planPDepot.setId(IdUtils.getId());
        planPDepot.setMUnit(planPDepotDto.getMunit());
        planPDepot.setDatayear(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());
        //planPDepot.setDatayear(year);

        ArrayList<PlanPDtml> planPDtmls = new ArrayList<>();
        // 处理仓库物资信息
        List<PlanPDtmlDto> planPDtmlDtos = planPDepotDto.getPlanPDtmlDtos();
        for (PlanPDtmlDto planPDtmlDto : planPDtmlDtos) {
            PlanPDtml planPDtml = new PlanPDtml();
            BeanUtil.copyProperties(planPDtmlDto, planPDtml);
            planPDtml.setId(IdUtils.getId());
            planPDtml.setPlanId(planPDepot.getId());
            planPDtml.setDepotName(planPDepot.getDepotName());
            planPDtmls.add(planPDtml);
        }
        planPDepotService.save(planPDepot);
        planPDtmlService.saveBatch(planPDtmls);

    }
}
